﻿package system.base;

import java.sql.ResultSet;
import java.util.*;

import system.data.BusMaterialAccountKilBase;
import framework.core.Conditions;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;

/*
 Author    :Benny
 CreateTime:2008-05-19 11:26:57
 */
public class BusMaterialAccountKil extends BusMaterialAccountKilBase {

    // ----system generated start----//
    public BusMaterialAccountKil() {

    }

    private static void delete(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        delete(con, TABLE_NAME, conditions);
    }

    public static BusMaterialAccountKil fetch() {
        return new BusMaterialAccountKil();
    }

    public static BusMaterialAccountKil copy(BusMaterialAccountKil srcObj) {
        BusMaterialAccountKil obj = fetch();
        obj.copyBase(srcObj);
        return obj;
    }

    public static BusMaterialAccountKil fetch(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        return fetch(con, conditions);
    }

    public static BusMaterialAccountKil fetch(DBConnection con, Conditions conditions) throws Exception {
        DBResult result = null;
        try {
            result = DBEngine.doSelect(con, "select * from " + TABLE_NAME, conditions);
            java.sql.ResultSet rs = result.getRs();
            BusMaterialAccountKil obj = null;
            if (rs.next()) {
                obj = fetch(rs);
            }
            return obj;
        } finally {
            if (result != null)
                result.clear();
        }
    }

    public static BusMaterialAccountKil fetch(java.sql.ResultSet rs) throws Exception {
        BusMaterialAccountKil obj = new BusMaterialAccountKil();
        obj.initObject(rs);
        return obj;
    }

    protected void getUnique(DBConnection con) throws Exception {
        setId(DBEngine.getId(con));
    }

    public static Vector getFullData(String sql, DBConnection con) throws Exception {
        Vector vec = new Vector();
        DBResult dbs = DBEngine.doSelect(con, sql);
        ResultSet rs = dbs.getRs();
        while (rs.next()) {
            String[] values = new String[7];
            values[0] = rs.getString(1);
            values[1] = rs.getString(2);
            values[2] = rs.getString(3);
            values[3] = rs.getString(4);
            values[4] = rs.getString(5);
            values[5] = rs.getString(6);
            values[6] = String.valueOf(rs.getDouble(7));
            vec.add(values);
        }
        rs.close();
        dbs.clear();
        return vec;
    }

    public static String[] getStr(Vector vec, int index) {
        String[] values = new String[vec.size()];
        for (int i = 0; i < vec.size(); i++) {
            String[] str = (String[]) vec.get(i);
            values[i] = str[index];
        }
        return values;
    }

    private static int getNumRows(ResultSet rs) throws Exception {
        int result = 0;
        if (rs.last()) {
            result = rs.getRow();
            rs.beforeFirst();// 光标回滚
        }
        return result;
    }

    private static String[] setStrArray(Vector vec) {
        int size = vec.size();
        String[] str = new String[size];
        for (int i = 0; i < size; i++) {
            str[i] = (String) vec.get(i);
        }
        return str;
    }

    public static String[] setStrArray(String[] str) {
        List list = Arrays.asList(str);
        Set set = new HashSet(list);
        String[] temp = new String[set.size()];
        Iterator ite = set.iterator();
        for (int i = 0; ite.hasNext(); i++) {
            temp[i] = (String) ite.next();
        }
        return temp;
    }

    public static String[] getValuesByIndexValueEquals(Vector vec, String str, int index1, int index2) {
        Vector temp = new Vector();
        for (int i = 0; i < vec.size(); i++) {
            String[] obj = (String[]) vec.get(i);
            if (obj[index1].equals(str)) {
                temp.add(obj[index2]);
            }
        }
        String[] needed = setStrArray(temp);
        needed = setStrArray(needed);
        return needed;
    }

    public static String[] getValuesByIndexValueEquals(Vector vec, String str1, String str2, int index1, int index2, int index3) {
        Vector temp = new Vector();
        for (int i = 0; i < vec.size(); i++) {
            String[] obj = (String[]) vec.get(i);
            if (obj[index1].equals(str1) && obj[index2].equals(str2)) {
                temp.add(obj[index3]);
            }
        }
        String[] needed = setStrArray(temp);
        needed = setStrArray(needed);
        return needed;
    }

    /** 算取金额 用于界面的同比环比 */
    public static double[] countMoney(double[] counts) {
        counts[6] = Math.abs(counts[1] - counts[5]);
        if (counts[5] > 0) {
            double temp = counts[1] - counts[5];
            temp = temp / counts[5];
            temp = temp * 100;
            counts[7] = temp;
        } else {
            counts[7] = 0.0;
        }
        counts[8] = Math.abs(counts[1] - counts[0]);
        if (counts[0] > 0) {
            double temp = counts[1] - counts[0];
            temp = temp / counts[0];
            temp = temp * 100;
            counts[9] = temp;
        } else {
            counts[9] = 0.0;
        }
        return counts;
    }

    private static String getResult(String sql, DBConnection con) throws Exception {
        DBResult dbs = DBEngine.doSelect(con, sql);
        ResultSet rs = dbs.getRs();
        String money = "";
        if (rs.next()) {
            double result = rs.getDouble(1);
            money = String.valueOf(result);
        } else {
            money = "0.0";
        }
        rs.close();
        dbs.clear();
        return money;
    }

    // 根据 年0000 月00 得到界面一系列所需金额*/
    public static String[] getMoneyValues(String year, String month, String busTypeCode, String warehouseCode, DBConnection con) throws Exception {
        String str = "select sum(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = ";
        String str1 = "select avg(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = ";
        int nowYear = Integer.parseInt(year);
        int nowMonth = Integer.parseInt(month);
        int lastYear = nowYear - 1;
        int lastMonth = nowMonth - 1;
        String lastYearStr = "" + lastYear;
        String lastMonthStr = "";
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        if (lastMonth < 10) {
            lastMonthStr = "0" + lastMonth;
        } else {
            lastMonthStr = "" + lastMonth;
        }
        String[] moneys = new String[10];
        String[] sql = new String[6];

        // 上月累计
        // 上月费用
        sql[0] = "";
        if (lastMonth == 12) {
            sql[0] = str + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + lastMonthStr + "%'";
        } else {
            sql[0] = str + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + lastMonthStr + "%'";
        }
        // 本月费用
        sql[1] = str + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + month + "%'";
        // 当年累计费用
        sql[2] = str + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 当年月平均费用
        sql[3] = str1 + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 去年月平均费用
        sql[4] = str1 + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + "%'";
        // 本月同比 得到去年本月的数据
        sql[5] = str + "'" + warehouseCode + "' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + month + "%'";

        for (int i = 0; i < sql.length; i++) {
            moneys[i] = getResult(sql[i], con);
            if (moneys[i] == null || moneys[i].equals("")) {
                moneys[i] = "0.0";
            }
        }

        moneys[6] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5])));
        if (Double.parseDouble(moneys[5]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5]);
            temp = temp / Double.parseDouble(moneys[5]);
            temp = temp * 100;
            moneys[7] = String.valueOf(temp);
        } else {
            moneys[7] = "0";
        }
        moneys[8] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0])));
        if (Double.parseDouble(moneys[0]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0]);
            temp = temp / Double.parseDouble(moneys[0]);
            temp = temp * 100;
            moneys[9] = String.valueOf(temp);
        } else {
            moneys[9] = "0";
        }
        return moneys;
    }

    // 根据 年0000 月00 得到界面一系列所需金额*/
    public static String[] getMoneyValues(String year, String month, String busTypeCode, DBConnection con) throws Exception {
        String str = "select sum(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where  ";
        String str1 = "select avg(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where  ";
        int nowYear = Integer.parseInt(year);
        int nowMonth = Integer.parseInt(month);
        int lastYear = nowYear - 1;
        int lastMonth = nowMonth - 1;
        String lastYearStr = "" + lastYear;
        String lastMonthStr = "";
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        if (lastMonth < 10) {
            lastMonthStr = "0" + lastMonth;
        } else {
            lastMonthStr = "" + lastMonth;
        }
        String[] moneys = new String[10];
        String[] sql = new String[6];

        // 上月累计
        // 上月费用
        sql[0] = "";
        if (lastMonth == 12) {
            sql[0] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + lastMonthStr + "%'";
        } else {
            sql[0] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + lastMonthStr + "%'";
        }
        // 本月费用
        sql[1] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + month + "%'";
        // 当年累计费用
        sql[2] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 当年月平均费用
        sql[3] = str1 + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 去年月平均费用
        sql[4] = str1 + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + "%'";
        // 本月同比 得到去年本月的数据
        sql[5] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + month + "%'";

        for (int i = 0; i < sql.length; i++) {
            moneys[i] = getResult(sql[i], con);
            if (moneys[i] == null || moneys[i].equals("")) {
                moneys[i] = "0.0";
            }
        }

        moneys[6] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5])));
        if (Double.parseDouble(moneys[5]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5]);
            temp = temp / Double.parseDouble(moneys[5]);
            temp = temp * 100;
            moneys[7] = String.valueOf(temp);
        } else {
            moneys[7] = "0";
        }
        moneys[8] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0])));
        if (Double.parseDouble(moneys[0]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0]);
            temp = temp / Double.parseDouble(moneys[0]);
            temp = temp * 100;
            moneys[9] = String.valueOf(temp);
        } else {
            moneys[9] = "0";
        }
        return moneys;
    }

    // ----add by Benny start----//
    // 根据 年0000 月00 得到界面一系列所需金额*/
    public static String[] getMoneyValues1(String year, String month, String busTypeCode,String BUS_CATEGORY_CODE, DBConnection con) throws Exception {
        String str = "select sum(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where  ";
        String str1 = "select avg(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where  ";
        int nowYear = Integer.parseInt(year);
        int nowMonth = Integer.parseInt(month);
        int lastYear = nowYear - 1;
        int lastMonth = nowMonth - 1;
        String lastYearStr = "" + lastYear;
        String lastMonthStr = "";
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        if (lastMonth < 10) {
            lastMonthStr = "0" + lastMonth;
        } else {
            lastMonthStr = "" + lastMonth;
        }
        String[] moneys = new String[10];
        String[] sql = new String[6];

        // 上月累计
        // 上月费用
        sql[0] = "";
        if (lastMonth == 12) {
            sql[0] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + lastYearStr + lastMonthStr + "%'";
        } else {
            sql[0] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + year + lastMonthStr + "%'";
        }
        // 本月费用
        sql[1] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + year + month + "%'";
        // 当年累计费用
        sql[2] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + year + "%'";
        // 当年月平均费用
        sql[3] = str1 + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + year + "%'";
        // 去年月平均费用
        sql[4] = str1 + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + lastYearStr + "%'";
        // 本月同比 得到去年本月的数据
        sql[5] = str + "BUS_TYPE_CODE = '" + busTypeCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and REPAIREN_TIME like '" + lastYearStr + month + "%'";

        for (int i = 0; i < sql.length; i++) {
            moneys[i] = getResult(sql[i], con);
            if (moneys[i] == null || moneys[i].equals("")) {
                moneys[i] = "0.0";
            }
        }

        moneys[6] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5])));
        if (Double.parseDouble(moneys[5]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5]);
            temp = temp / Double.parseDouble(moneys[5]);
            temp = temp * 100;
            moneys[7] = String.valueOf(temp);
        } else {
            moneys[7] = "0";
        }
        moneys[8] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0])));
        if (Double.parseDouble(moneys[0]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0]);
            temp = temp / Double.parseDouble(moneys[0]);
            temp = temp * 100;
            moneys[9] = String.valueOf(temp);
        } else {
            moneys[9] = "0";
        }
        return moneys;
    }
    // ----add by Benny end----//
 // 根据 年0000 月00 得到界面一系列所需金额*/
    public static String[] getMoneyValues1(String year, String month, String busTypeCode,String BUS_CATEGORY_CODE, String warehouseCode, DBConnection con) throws Exception {
        String str = "select sum(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = ";
        String str1 = "select avg(KIL_REPAIRED_MONEY) from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = ";
        int nowYear = Integer.parseInt(year);
        int nowMonth = Integer.parseInt(month);
        int lastYear = nowYear - 1;
        int lastMonth = nowMonth - 1;
        String lastYearStr = "" + lastYear;
        String lastMonthStr = "";
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        if (lastMonth < 10) {
            lastMonthStr = "0" + lastMonth;
        } else {
            lastMonthStr = "" + lastMonth;
        }
        String[] moneys = new String[10];
        String[] sql = new String[6];

        // 上月累计
        // 上月费用
        sql[0] = "";
        if (lastMonth == 12) {
            sql[0] = str + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + lastMonthStr + "%'";
        } else {
            sql[0] = str + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + lastMonthStr + "%'";
        }
        // 本月费用
        sql[1] = str + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + month + "%'";
        // 当年累计费用
        sql[2] = str + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 当年月平均费用
        sql[3] = str1 + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + year + "%'";
        // 去年月平均费用
        sql[4] = str1 + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + "%'";
        // 本月同比 得到去年本月的数据
        sql[5] = str + "'" + warehouseCode + "' and BUS_CATEGORY_CODE = '"+BUS_CATEGORY_CODE+"' and BUS_TYPE_CODE = '" + busTypeCode + "' and REPAIREN_TIME like '" + lastYearStr + month + "%'";

        for (int i = 0; i < sql.length; i++) {
            moneys[i] = getResult(sql[i], con);
            if (moneys[i] == null || moneys[i].equals("")) {
                moneys[i] = "0.0";
            }
        }

        moneys[6] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5])));
        if (Double.parseDouble(moneys[5]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[5]);
            temp = temp / Double.parseDouble(moneys[5]);
            temp = temp * 100;
            moneys[7] = String.valueOf(temp);
        } else {
            moneys[7] = "0";
        }
        moneys[8] = String.valueOf(Math.abs(Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0])));
        if (Double.parseDouble(moneys[0]) > 0) {
            double temp = Double.parseDouble(moneys[1]) - Double.parseDouble(moneys[0]);
            temp = temp / Double.parseDouble(moneys[0]);
            temp = temp * 100;
            moneys[9] = String.valueOf(temp);
        } else {
            moneys[9] = "0";
        }
        return moneys;
    }
}
